Skip to content

fix(move-compiler): prevent panic on assert! with wrong arg count#25471

Open
lau90eth wants to merge 1 commit intoMystenLabs:mainfrom
lau90eth:fix-assert-panic-25459
Open

fix(move-compiler): prevent panic on assert! with wrong arg count#25471
lau90eth wants to merge 1 commit intoMystenLabs:mainfrom
lau90eth:fix-assert-panic-25459

Conversation

@lau90eth
Copy link

@lau90eth lau90eth commented Feb 14, 2026

[move-compiler] Fix compiler panics: assert! args and PackVariant fields

Fixes

Summary

This PR fixes two compiler panics in hlir/translate.rs caused by improper use of .unwrap() on operations that can fail. Both fixes replace unwrap() with proper error handling to provide graceful error messages instead of crashing.

Fix 1: assert! Argument Count (Issue #25459)

Problem: The compiler panicked with called Result::unwrap() on an Err value when assert! was called with more than 2 arguments.

Root Cause: In hlir/translate.rs, the code used .unwrap() when converting the argument list to a fixed-size array [T; 2]:

E::ExpList(arg_list) => arg_list.try_into().unwrap(),

Solution: Replaced .unwrap() with proper error handling using match:
E::ExpList(arg_list) => match arg_list.try_into() {
    Ok(arr) => arr,
    Err(_) => {
        context
            .env
            .add_diag(ice!((eloc, "assert! expects 1 or 2 arguments, but got a different number")));
        return error_exp(eloc);
    }
},

Changes:
Fixed 2 locations in hlir/translate.rs (lines ~905 and ~943)
Added test case bad_assert_args to prevent regression
Testing:
assert!(true, 0, 1) now produces a graceful error instead of panic
Valid cases (assert!(true) and assert!(true, 0)) continue to work
Fix 2: PackVariant Field Lookup (Issue #25453)
Problem: The compiler panicked with called Option::unwrap() on a None value when a malformed enum variant declaration (with parser-recovered fields) was used in a PackVariant expression.
Example that caused panic:
module 0x0::T {
    enum E {
        V x{: u8},  // Malformed syntax (missing comma)
    }
    fun f(): E {
        E::V { x: 0 }  // Compiler crashed here
    }
}

Root Cause: In hlir/translate.rs, the code used .unwrap() when looking up fields in the field map:
.map(|(f, (exp_idx, (bt, tf)))| {
    (*field_map.get(&f).unwrap(), f, exp_idx, bt, tf)
})

Solution: Replaced .map().unwrap() with .filter_map() + match:
.filter_map(|(f, (exp_idx, (bt, tf)))| {
    match field_map.get(&f) {
        Some(decl_idx) => Some((*decl_idx, f, exp_idx, bt, tf)),
        None => {
            context.add_diag(diag!(
                TypeSafety::InvalidField,
                (f.loc(), format!(
                    "Invalid field '{}' in enum variant constructor",
                    f.value()
                ))
            ));
            None
        }
    }
})

Changes:
Fixed 1 location in hlir/translate.rs (line ~1455)
Now shows clear error message instead of crashing
Testing:
Malformed enum variants now produce proper error diagnostics
Valid enum variant constructors continue to work correctly
Checklist
[x] Both fixes follow Rust error handling best practices
[x] Error messages are clear and actionable
[x] Valid code continues to compile without issues
[x] Fixes are minimal and focused on the specific issues

@vercel
Copy link

vercel bot commented Feb 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
sui-docs Ready Ready Preview, Comment Feb 14, 2026 11:43pm
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
multisig-toolkit Ignored Ignored Preview Feb 14, 2026 11:43pm
sui-kiosk Ignored Ignored Preview Feb 14, 2026 11:43pm

Request Review

@vercel
Copy link

vercel bot commented Feb 14, 2026

Deployment failed with the following error:

Invalid vercel.json file provided

Fixes MystenLabs#25459
Fixes MystenLabs#25453

This PR fixes two compiler panics in hlir/translate.rs caused by improper
use of .unwrap() on operations that can fail.

Fix 1: assert! argument count (Issue MystenLabs#25459)
- Replace unwrap() with match when converting arg list to fixed-size array
- Now shows error instead of panic when assert! has wrong arg count

Fix 2: PackVariant field lookup (Issue MystenLabs#25453)
- Replace map().unwrap() with filter_map() + match
- Now shows error instead of panic when enum variant has invalid fields

Both fixes provide graceful error messages instead of crashing.
@lau90eth lau90eth force-pushed the fix-assert-panic-25459 branch from 866d927 to d401020 Compare February 14, 2026 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant